//
//  HavePayOrderDetailViewController.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/4/28.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "HavePayOrderDetailViewController.h"
#import "CustomPayedOrderHeadTableViewCell.h"
#import "CustomShipInfoTableViewCell.h"
#import "CustomProductTotalTableViewCell.h"
#import "CustomOrderCommenTableViewCell.h"
#import "CustomOrderProductTableViewCell.h"
#import "ExpressDetailViewController.h"
@interface HavePayOrderDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation HavePayOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviTitle:@"订单详情"];
    [self setNaviLeftButton:@""];
    [self loadOrderData];
    // Do any additional setup after loading the view.
}
-(void)loadOrderData{
    [[LKDHud shareHud] showLoadingView];
    __weak typeof (self) weakSelf = self;
    [ApiClient getOrderDetailOrderID:self.orderID didFinishLoaded:^(OrderDetailModel *model, NSString *path) {
        [[LKDHud shareHud] hideLoadingView];
        _orderModel=model.data;
        
        if (![weakSelf.view.subviews containsObject:weakSelf.displayView]) {
            [weakSelf.view addSubview:weakSelf.displayView];
        }else{
            [weakSelf.displayView reloadData];
        }
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        [[LKDHud shareHud] hideLoadingView];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UITableView*)displayView{
    if (!_displayView) {
        _displayView=[[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        _displayView.delegate=self;
        _displayView.dataSource=self;
        _displayView.backgroundColor=[UIColor clearColor];
        _displayView.tableFooterView=[[UIView alloc] initWithFrame:CGRectZero];
        // [_displayView setSeparatorColor:[UIColor redColor]];
    }
    return _displayView;
}
#pragma mark-UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_orderModel.express) {
        return 2+_orderModel.products.count+_orderModel.totals.count;
    }else{
        
    }
    return 1+_orderModel.products.count+_orderModel.totals.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_orderModel.express) {
        if (indexPath.row==0) {
            static NSString *identifier = @"CustomPayedOrderHeadTableViewCell";
            CustomPayedOrderHeadTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                                       identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomPayedOrderHeadTableViewCell"  owner:self options:nil] lastObject];
                
            }
            [cell loadDataModel:_orderModel];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            return cell;
            
        }else if (indexPath.row==1){
            static NSString *identifier = @"CustomShipInfoTableViewCell";
            CustomShipInfoTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                                     identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomShipInfoTableViewCell"  owner:self options:nil] lastObject];
                
            }
            [cell loadData:_orderModel.express];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            return cell;

        }else if (indexPath.row>1&&indexPath.row<_orderModel.products.count+2){
            static NSString *identifier = @"CustomOrderProductTableViewCell";
            CustomOrderProductTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                                     identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomOrderProductTableViewCell"  owner:self options:nil] lastObject];
                
            }
            OrderProductModel *productModel=_orderModel.products[indexPath.row-2];
            [cell loadProductModel:productModel];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            return cell;
            
        }else if (indexPath.row==_orderModel.products.count+2){
            static NSString *identifier = @"CustomProductTotalTableViewCell";
            CustomProductTotalTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                                     identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomProductTotalTableViewCell"  owner:self options:nil] lastObject];
                
            }
            TotalsModel *totalModel=_orderModel.totals[indexPath.row-_orderModel.products.count-2];
            [cell loadTotalModel:totalModel];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            return cell;
            
        }else{
            static NSString *identifier = @"CustomOrderCommenTableViewCell";
            CustomOrderCommenTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                                    identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomOrderCommenTableViewCell"  owner:self options:nil] lastObject];
                
            }
            TotalsModel *totalModel=_orderModel.totals[indexPath.row-_orderModel.products.count-2];
            [cell loadTotalModel:totalModel];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            return cell;
            
        }

    }else{
        if (indexPath.row==0) {
            static NSString *identifier = @"CustomPayedOrderHeadTableViewCell";
            CustomPayedOrderHeadTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                                       identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomPayedOrderHeadTableViewCell"  owner:self options:nil] lastObject];
                
            }
            [cell loadDataModel:_orderModel];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            return cell;
            
        }else if (indexPath.row>0&&indexPath.row<_orderModel.products.count+1){
            static NSString *identifier = @"CustomOrderProductTableViewCell";
            CustomOrderProductTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                                 identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomOrderProductTableViewCell"  owner:self options:nil] lastObject];
                
            }
            OrderProductModel *productModel=_orderModel.products[indexPath.row-1];
            [cell loadProductModel:productModel];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            return cell;
            
        }else if (indexPath.row==_orderModel.products.count+1){
            static NSString *identifier = @"CustomProductTotalTableViewCell";
            CustomProductTotalTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                                     identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomProductTotalTableViewCell"  owner:self options:nil] lastObject];
                
            }
            TotalsModel *totalModel=_orderModel.totals[indexPath.row-_orderModel.products.count-1];
            [cell loadTotalModel:totalModel];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            return cell;

        }else{
            static NSString *identifier = @"CustomOrderCommenTableViewCell";
            CustomOrderCommenTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                                     identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomOrderCommenTableViewCell"  owner:self options:nil] lastObject];
                
            }
            TotalsModel *totalModel=_orderModel.totals[indexPath.row-_orderModel.products.count-1];
            [cell loadTotalModel:totalModel];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            return cell;
            
        }

    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_orderModel.express) {
        if (indexPath.row==0) {
            return 230;
        }else if (indexPath.row==1){
            return 96;
        }else if (indexPath.row>1&&indexPath.row<_orderModel.products.count+2){
            return 90;
        }else if (indexPath.row==_orderModel.products.count+2){
            return 50;
        }else{
            return 44;
        }
        
    }else{
        if (indexPath.row==0) {
            return 230;
        }else if (indexPath.row>0&&indexPath.row<_orderModel.products.count+1){
            return 90;
        }else if (indexPath.row==_orderModel.products.count+1){
            return 50;
        }else{
            return 44;
        }
        
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_orderModel.express) {
        if (indexPath.row==1) {
            ExpressDetailViewController *expressVC=[[ExpressDetailViewController alloc] init];
            expressVC.orderID=self.orderID;
            [self.navigationController pushViewController:expressVC animated:YES];
        }
    }
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
